package com.gqzmy.springboot3.controller;

import com.github.pagehelper.PageInfo;
import com.gqzmy.springboot3.entity.User;
import com.gqzmy.springboot3.exceptions.SystemApiException;
import com.gqzmy.springboot3.service.UserService;
import com.gqzmy.springboot3.service.dto.ServiceResultDTO;
import com.gqzmy.springboot3.vo.LoginUser;
import com.gqzmy.springboot3.vo.UserQuery;
import com.gqzmy.springboot3.vo.UserRegister;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 周小哥  gqzmy@163.com 2022/12/1 11:10
 */

@RequestMapping("/user")
@RestController
public class UserController {

    public static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    /**
     * 新增用户
     *
     * @param userRegister 新增用户对象
     * @return 返回注册后的用户ID
     */
    @PostMapping("/register")
    public ResponseEntity<String> register(@Validated @RequestBody UserRegister userRegister) {
        try {
            ServiceResultDTO<User> register = userService.register(userRegister);
            return register.isSuccess()
                    ? ResponseEntity.ok(register.getData().getId())
                    : ResponseEntity.badRequest().body(register.getMessage());
        } catch (SystemApiException e) {
            return ResponseEntity.badRequest().body(e.getErrAbstract());
        }
    }

    @PostMapping("/login")
    public ResponseEntity<Boolean> login(@Validated @RequestBody LoginUser loginUser) {
        logger.info("loginUser-{}", loginUser);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id查询用户信息
     *
     * @param id 用户id
     * @return 用户对象
     */
    @GetMapping("/getById/{id}")
    public ResponseEntity<User> getById(@PathVariable("id") String id) {
        return ResponseEntity.ok(userService.getById(id));
    }

    /**
     * 根据name查询用户信息
     *
     * @param name 用户名
     * @return 用户对象
     */
    @GetMapping("/getByName/{id}")
    public ResponseEntity<User> getByName(@PathVariable("id") String name) {
        return ResponseEntity.ok(userService.getByName(name));
    }

    /**
     * 更新用户
     *
     * @param user 要更新的对象信息
     * @return 更新后的对象信息
     */
    @PutMapping("/update")
    public ResponseEntity<User> updateUser(@RequestBody User user) {
        return new ResponseEntity<>(userService.updateUser(user), HttpStatus.OK);
    }

    /**
     * 根据条件查询用户集合
     *
     * @param userQuery 条件对象
     * @return 用户集合
     */
    @PostMapping("/getUserPageInfo")
    public ResponseEntity<PageInfo<User>> getUserPageInfo(@RequestBody UserQuery userQuery) {
        PageInfo<User> userPageInfo = userService.getUserPageInfo(userQuery);
        return new ResponseEntity<>(userPageInfo, HttpStatus.OK);
    }
}
